package cn.zb.service.impl;

import cn.zb.api.user.feign.UserFeign;
import cn.zb.api.user.vo.DeptVO;
import cn.zb.api.user.vo.RoleVO;
import cn.zb.api.user.vo.UserVO;
import org.dromara.warm.flow.ui.dto.HandlerFunDto;
import org.dromara.warm.flow.ui.dto.HandlerQuery;
import org.dromara.warm.flow.ui.dto.TreeFunDto;
import org.dromara.warm.flow.ui.service.HandlerSelectService;
import org.dromara.warm.flow.ui.vo.HandlerSelectVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 流程图办理人权限数据获取
 *
 * @author:
 * @date: 2024-11-26 16:11
 **/
@Service
public class HandlerSelectServiceImpl implements HandlerSelectService {
    @Resource
    UserFeign userFeign;

    @Override
    public List<String> getHandlerType() {
        return Arrays.asList("用户", "角色", "部门");
    }

    @Override
    public HandlerSelectVo getHandlerSelect(HandlerQuery query) {
        if ("角色".equals(query.getHandlerType())) {
            return getRole(query);
        }

        if ("部门".equals(query.getHandlerType())) {
            return getDept(query);
        }

        if ("用户".equals(query.getHandlerType())) {
            return getUser(query);
        }

        return new HandlerSelectVo();
    }

    private HandlerSelectVo getUser(HandlerQuery query) {
        List<UserVO> users = userFeign.getAllUsers(query.getGroupId()).getData();
        // 查询部门列表，构建树状结构
        List<DeptVO> depts = userFeign.getAllDepts().getData();
        // 业务系统数据，转成组件内部能够显示的数据, total是业务数据总数，用于分页显示
        HandlerFunDto<UserVO> handlerFunDto = new HandlerFunDto<>(users, users.size())
                .setStorageId(user -> "user:"+user.getUserId().toString())
                .setHandlerCode(UserVO::getUserName) // 权限编码
                .setHandlerName(UserVO::getUserName) // 权限名称
                .setGroupName(user -> user.getDeptName()!=null ? user.getDeptName() : "");

        // 业务系统机构，转成组件内部左侧树列表能够显示的数据
        TreeFunDto<DeptVO> treeFunDto = new TreeFunDto<>(depts)
                .setId(dept -> dept.getDeptId().toString()) // 左侧树ID
                .setName(DeptVO::getDeptName) // 左侧树名称
                .setParentId(dept -> dept.getParentId().toString()); // 左侧树父级ID

        return getHandlerSelectVo(handlerFunDto, treeFunDto);
    }

    private HandlerSelectVo getDept(HandlerQuery query) {
        List<DeptVO> depts = userFeign.getAllDepts().getData();
        // 业务系统数据，转成组件内部能够显示的数据, total是业务数据总数，用于分页显示
        HandlerFunDto<DeptVO> handlerFunDto = new HandlerFunDto<>(depts, depts.size())
                .setStorageId(dept -> "dept:" + dept.getDeptId()) // 前面拼接dept:  是为了防止用户、部门的主键重复
                .setHandlerCode(dept->dept.getDeptId().toString()) // 权限编码
                .setHandlerName(DeptVO::getDeptName) // 权限名称
                .setCreateTime(DeptVO::getCreateTime); // 创建时间

        return getHandlerSelectVo(handlerFunDto);
    }

    private HandlerSelectVo getRole(HandlerQuery query) {
        List<RoleVO> roles = userFeign.getAllRoles().getData();

        // 业务系统数据，转成组件内部能够显示的数据, total是业务数据总数，用于分页显示
        HandlerFunDto<RoleVO> handlerFunDto = new HandlerFunDto<>(roles, roles.size())
                // 以下设置获取内置变量的Function
                .setStorageId(role -> "role:" + role.getRoleName()) // 前面拼接role:  是为了防止用户、角色的主键重复
                .setHandlerCode(RoleVO::getRoleName) // 权限编码
                .setHandlerName(RoleVO::getRoleName) // 权限名称
                ;

        return getHandlerSelectVo(handlerFunDto);
    }
}
